package com.mzy.linear_struct.ks.链表;

/**
 * @Author: mizy3
 * @Date: 2024/08/22/上午11:10
 * @Description:
 */
public class 返回倒数第k个节点 {
    public int kthToLast(ListNode head, int k) {
        ListNode cur = head;
        //       s
        // 1 2 3 4 5     k = 2
        //           f
        while (k > 0) {
            cur = cur.next;
            k--;
        }

        while (cur != null){
            head = head.next;
            cur = cur.next;
        }

        return head.val;
    }
}
